package com.lmuse.xmr.foodspotting.ui.userpage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.db.FollowPresenterImpl;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.ui.login.LoginActivity;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;

import java.util.List;

import cn.bmob.v3.exception.BmobException;

public class UserPageFragment extends Fragment implements Contract.UserView, Contract.FollowView{

    private LinearLayout linearLayout;
    private TextView nickNameDis;
    private TextView followingTv;
    private TextView followerTv;
    private Button myCollect;
    private Button myUpload;

    private Contract.UserPresenter userPresenter;
    private Contract.FollowPresenter followPresenter;

    private User user;
    private boolean isLogin = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_userpage, container, false);

        userPresenter = new UserPresenterImpl(this);
        followPresenter = new FollowPresenterImpl(this);
        initView(view);
        return view;
    }

    void initView(View view) {
        linearLayout = (LinearLayout) view.findViewById(R.id.ll_follow);
        nickNameDis = (TextView) view.findViewById(R.id.tv_nickname);
        followingTv = (TextView) view.findViewById(R.id.tv_following_dis);
        followerTv = (TextView) view.findViewById(R.id.tv_follower_dis);
        myCollect = (Button) view.findViewById(R.id.btn_my_collection);
        myUpload = (Button) view.findViewById(R.id.btn_my_upload);
        nickNameDis.setOnClickListener(new ButtonClickListener());
        followingTv.setOnClickListener(new ButtonClickListener());
        followerTv.setOnClickListener(new ButtonClickListener());
        myCollect.setOnClickListener(new ButtonClickListener());
        myUpload.setOnClickListener(new ButtonClickListener());
    }

    class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_nickname:

                    if (isLogin) {
                        Intent intent = new Intent(getActivity(), SettingActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    }
                    break;
                case R.id.tv_following_dis:
                    //todo
                    break;
                case R.id.tv_follower_dis:
                    //todo
                    break;
                case R.id.btn_my_collection:

                    if (isLogin) {
                        Intent intent = new Intent(getActivity(), MyCollectsActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    }
                    break;
                case R.id.btn_my_upload:

                    if (isLogin) {
                        Intent intent = new Intent(getActivity(), MyUploadActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    }
                    break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        userPresenter.getUserInfoCache();
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {

    }

    @Override
    public void onSignInFailed(String msg) {

    }

    @Override
    public void onUserInfoCache(User user) {

        if (user != null) {
            isLogin = true;
            this.user = user;
            if (user.getNickname() != null) {
                nickNameDis.setText(user.getNickname());
            }else {
                nickNameDis.setText(user.getUsername());
            }
            linearLayout.setVisibility(View.VISIBLE);

            followPresenter.queryFollowings();
            if (user.getUsername().equals("admin")) {
                userPresenter.queryUserAmount();
            }else {
                followPresenter.queryMyFollowersAmount();
            }

        }else {
            nickNameDis.setText("登录/注册");
            linearLayout.setVisibility(View.GONE);
            isLogin = false;
        }
    }

    @Override
    public void onUpdateUserSuccess() {

    }

    @Override
    public void onUpdateUserFailed(String msg) {

    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {

    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {

    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {

    }

    @Override
    public void onQueryUserAmountSuccess(int size) {
        followerTv.setText(size + "");
    }

    @Override
    public void onQueryUserAmountFailed(String msg) {
        Toast.makeText(getActivity(), "获取粉丝数失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAddFollowingSuccess() {

    }

    @Override
    public void onAddFollowingFailed(String msg) {

    }

    @Override
    public void onUnFollowingSuccess() {

    }

    @Override
    public void onUnFollowingFailed(String msg) {

    }

    @Override
    public void onQueryFollowingSuccess(List<User> userList) {
        followingTv.setText(userList.size() + "");
    }

    @Override
    public void onQueryFollowingFailed(String msg) {
        Toast.makeText(getActivity(), "获取我的关注失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onQueryFollowerSuccess(List<User> userList) {

    }

    @Override
    public void onQueryFollowerFailed(String msg) {

    }

    @Override
    public void onQueryFollowerAmountSuccess(Integer num) {
        followerTv.setText(num + "");
    }

    @Override
    public void onQueryFollowerAmountFailed(String msg) {
        Toast.makeText(getActivity(), "获取我的粉丝失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {

    }

    @Override
    public void onQueryByKeyFailed(String msg) {

    }
}
